package com.letoken.platform.app.job;

import com.letoken.platform.app.configuration.PancakeSwapLPQuery;
import com.letoken.platform.app.service.AppUserWalletService;
import com.letoken.platform.pub.enums.LetokenTokenTypeEnum;
import com.letoken.platform.pub.enums.TokenTransactionTypeEnum;
import com.letoken.platform.repo.app.dao.AppFundInfoMapper;
import com.letoken.platform.repo.app.dao.AppLpLockHistoryMapper;
import com.letoken.platform.repo.mall.po.AppLpLockHistory;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Component
public class UpdateLPJob {

    @Resource
    AppLpLockHistoryMapper appLpLockHistoryMapper;

    @Autowired
    private AppUserWalletService appUserWalletService;
    private BigDecimal userRate = new BigDecimal("0.1");
    private BigDecimal upRate = new BigDecimal("0.03");

    private BigDecimal amounts = new BigDecimal("1000");
    @Autowired
    private AppFundInfoMapper appFundInfoMapper;

   // @XxlJob("updateLP")
    public  void updateLP() throws Exception {
      List<AppLpLockHistory> list = appLpLockHistoryMapper.getLockLp();
        for (AppLpLockHistory appLpLockHistory: list) {
            addCAP(appLpLockHistory.getUserId(),appLpLockHistory.getAmount());
        }
    }

    @Transactional
    public void addCAP(Integer userId, BigDecimal amount) throws Exception {
        PancakeSwapLPQuery.Price price = PancakeSwapLPQuery.getPrice();
        BigDecimal lpPrice = amount.multiply(price.getLp());
        BigDecimal bigDecimal = lpPrice.divide(price.getCap(),8, RoundingMode.DOWN);
        appUserWalletService.updateWallet(LetokenTokenTypeEnum.CAP.getId(),userId,bigDecimal.multiply(userRate), TokenTransactionTypeEnum.CAP_airdrop.getValue(),"");

        Integer upId = appFundInfoMapper.getLevel(userId);
        if(upId !=null && !upId.equals(0)){
            appUserWalletService.updateWallet(LetokenTokenTypeEnum.CAP.getId(),upId,bigDecimal.multiply(upRate),TokenTransactionTypeEnum.CAP_airdrop_Community.getValue(),"");
        }
    }
}
